package com.abel.demo.utils;

import java.util.*;

/**
 * Author: Abel.lin
 * Date: 2021/6/30 11:20
 * Company: Abel.Studio
 * Description:
 */
public class ToolsUtil {
    public static int randomInt(int min, int max){
        int n = new Random().nextInt(max - min + 1) + min;
        return n;
    }

    public static Set<Integer> randomInt(int min, int max, int count){
        Set<Integer> set = new HashSet<>();
        int len = max-min+1;

        List<Integer> sources = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            sources.add(min + i);
        }
        if(count > len){
            set = new HashSet<>(sources);
            return set;
        }

        for (int i = 0; i < count; i++) {
            int index = new Random().nextInt(sources.size());
            set.add(sources.get(index));
            sources.remove(index);
        }
        return set;
    }

    public static int strToInt(String str){
        Integer n = 0;
        try {
            n = Integer.parseInt(str);
        }catch (Exception e){}

        return n;
    }

    public static boolean isEmpty(String str){
        if(str == null || str.trim().length() == 0){
            return true;
        }
        return false;
    }
    public static boolean isEmpty(Collection<?> list){
        if(list == null || list.isEmpty()){
            return true;
        }
        return false;
    }

    public static String substringBefore(String str, String separator) {
        if (!isEmpty(str) && !isEmpty(separator)) {
            int pos = str.indexOf(separator);
            return pos == -1 ? str : str.substring(0, pos);
        } else {
            return str;
        }
    }

    public static String substringAfter(String str, String separator) {
        if (!isEmpty(str) && !isEmpty(separator)) {
            int pos = str.indexOf(separator);
            return pos == -1 ? "" : str.substring(pos + separator.length());
        } else {
            return str;
        }
    }

    public static String substringAfterLast(String str, String separator) {
        if (isEmpty(str)) {
            return str;
        } else if (isEmpty(separator)) {
            return "";
        } else {
            int pos = str.lastIndexOf(separator);
            return pos != -1 && pos != str.length() - separator.length() ? str.substring(pos + separator.length()) : "";
        }
    }

    public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        } else {
            if (end < 0) {
                end += str.length();
            }

            if (start < 0) {
                start += str.length();
            }

            if (end > str.length()) {
                end = str.length();
            }

            if (start > end) {
                return "";
            } else {
                if (start < 0) {
                    start = 0;
                }

                if (end < 0) {
                    end = 0;
                }

                return str.substring(start, end);
            }
        }
    }

    public static int compareApiVersion(String v1, String v2){
        if(isEmpty(v1) || isEmpty(v2)){
            return 1;
        }

        v1 = v1.replaceAll("\\.", "");
        v2 = v2.replaceAll("\\.", "");

        int n = v1.length() - v2.length();
        if(n < 0){
            v1 = String.format("%s%0"+(-n)+"d", v1, 0);
        }else if(n > 0){
            v2 = String.format("%s%0"+n+"d", v2, 0);
        }

        return Integer.compare(Integer.parseInt(v1), Integer.parseInt(v2));
    }
}
